package com.edol.painting.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

import com.edol.painting.R;
import com.edol.painting.utils.Constant;
import com.edol.painting.utils.PainterUtils;

public class BrushAdjusterView extends LinearLayout {

    private BrushAdjusterPreview mPreviewView;
    private CustomSeekBar mBrushWidthSeekBar;
    private CustomSeekBar mBrushAlphaSeekBar;
    private OnBrushWidthChangedListener mBrushWidthChangedListener;
    private OnBrushAlphaChangedListener mBrushAlphaChangedListener;

    private int mBrushMinWidth;
    private int mBrushMaxWidth;
    private int mBrushMinAlpha;
    private int mBrushMaxAlpha;

    public interface OnBrushWidthChangedListener {
        void brushWidthChanged(int brushWidth);
        void brushWidthEnd(int brushWidth);
    }

    public interface OnBrushAlphaChangedListener {
        void brushAlphaChanged(int brushAlpha);
        void brushAlphaEnd(int brushAlpha);
    }

    public interface OnBrushTypeChangedListener {
        void brushTypeChanged(int brushType);
    }

    public BrushAdjusterView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public BrushAdjusterView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BrushAdjusterView(Context context) {
        super(context);
    }

    public void inflateView(View parent) {
        mBrushWidthSeekBar = (CustomSeekBar) parent.findViewById(R.id.brushtool_width_seekbar);
        mBrushWidthSeekBar.setProgressChangedListener(mBrushWidthSeekBarChangedListener);
        mBrushWidthSeekBar.setMax(Constant.BRUSH_WIDTH_SEEKBAR_MAX);

        mBrushAlphaSeekBar = (CustomSeekBar) parent.findViewById(R.id.brushtool_alpha_seekbar);
        mBrushAlphaSeekBar.setProgressChangedListener(mBrushAlphaSeekBarChangedListener);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

    public void setCurrentColor(int color) {
        mBrushAlphaSeekBar.setCurrentColor(color);
        mPreviewView.setBrushColor(color);
    }

    public void setPreview(BrushAdjusterPreview view) {
        mPreviewView = view;
    }
    
    public void setBrushWidthSeekBarVisible(boolean visible) {
    	boolean isShow = mBrushWidthSeekBar.getVisibility() == View.VISIBLE;
    	if (visible && !isShow) {
    		mBrushWidthSeekBar.setVisibility(View.VISIBLE);
    	} else if (!visible && isShow) {
    		mBrushWidthSeekBar.setVisibility(View.GONE);
    	}
    }

//    public void setBrush(Brush brush) {
//        mPreviewView.setBrush(brush);
//    }

    public void setBrushType(int brushType) {
/*        if (mBrushes != null && mSelectedBrush != brushType) {
            mSelectedBrush = brushType;
            updateBrushIcon();
        }*/
    }

    public void setBrushWidthChangedListener(
            OnBrushWidthChangedListener listener) {
        mBrushWidthChangedListener = listener;
    }

    public void setBrushAlphaChangedListener(
            OnBrushAlphaChangedListener listener) {
        mBrushAlphaChangedListener = listener;
    }

    public void setBrushWidthRange(int minValue, int maxValue) {
        mBrushMinWidth = minValue;
        mBrushMaxWidth = maxValue;
    }

    public void setBrushAlphaRange(int minValue, int maxValue) {
        mBrushMinAlpha = PainterUtils.getAlphaCurveIndex(minValue);
        mBrushMaxAlpha = PainterUtils.getAlphaCurveIndex(maxValue);
    }

    public void initialBrushStatus(int brushWidth, int brushAlpha) {
        if ((mBrushMaxWidth - mBrushMinWidth) != 0) {
            mBrushWidthSeekBar.setProgress((brushWidth - mBrushMinWidth) * mBrushWidthSeekBar.getMax() / (mBrushMaxWidth - mBrushMinWidth));
            mBrushAlphaSeekBar.setProgress((brushAlpha - mBrushMinAlpha) * mBrushAlphaSeekBar.getMax() / (mBrushMaxAlpha - mBrushMinAlpha));
        }
    }
    
    public void initialBrushAlpha(int brushAlpha) {
    	mBrushAlphaSeekBar.setProgress((brushAlpha - mBrushMinAlpha) * mBrushAlphaSeekBar.getMax() / (mBrushMaxAlpha - mBrushMinAlpha));
    }

    private CustomSeekBar.OnProgressChangedListener mBrushWidthSeekBarChangedListener = new CustomSeekBar.OnProgressChangedListener() {
        @Override
        public void onProgressChanged(CustomSeekBar seekBar,int progress) {
            int width = progress * (mBrushMaxWidth - mBrushMinWidth) / seekBar.getMax() + mBrushMinWidth;
            if (mBrushWidthChangedListener != null) {
                mBrushWidthChangedListener.brushWidthChanged(width);
            }
            mPreviewView.setBrushWidth(width);
            mPreviewView.invalidate();
        }

		@Override
		public void onProgressEnd(CustomSeekBar seekBar, int progress) {
			 int width = progress * (mBrushMaxWidth - mBrushMinWidth) / seekBar.getMax() + mBrushMinWidth;
	            if (mBrushWidthChangedListener != null) {
	                mBrushWidthChangedListener.brushWidthEnd(width);
	            }
	            mPreviewView.setBrushWidth(width);
	            mPreviewView.invalidate();
		}
    };

    private CustomSeekBar.OnProgressChangedListener mBrushAlphaSeekBarChangedListener = new CustomSeekBar.OnProgressChangedListener() {
        @Override
        public void onProgressChanged(CustomSeekBar seekBar,int progress) {
            int alpha = progress * (mBrushMaxAlpha - mBrushMinAlpha) / seekBar.getMax() + mBrushMinAlpha;
            if (mBrushAlphaChangedListener != null) {
                mBrushAlphaChangedListener.brushAlphaChanged(alpha);
            }
            mPreviewView.setBrushAlpha(alpha);
            mPreviewView.invalidate();
        }

		@Override
		public void onProgressEnd(CustomSeekBar seekBar, int progress) {
			  int alpha = progress * (mBrushMaxAlpha - mBrushMinAlpha) / seekBar.getMax() + mBrushMinAlpha;
	            if (mBrushAlphaChangedListener != null) {
	                mBrushAlphaChangedListener.brushAlphaEnd(alpha);
	            }
	            mPreviewView.setBrushAlpha(alpha);
	            mPreviewView.invalidate();
		}
    };

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return true;
    }

    public void invalidatePreview() {
    	if (mPreviewView != null) {
    		mPreviewView.invalidate();
    	}
    }
    
    public void setSeekBarEnabled(boolean b) {
    	mBrushWidthSeekBar.setOnTouchClickable(b);
    	mBrushAlphaSeekBar.setOnTouchClickable(b);
    }
    
    public void clear() {
    	mBrushWidthSeekBar.clear();
    	mBrushAlphaSeekBar.clear();
    }

}